<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['ischecklogin']=cfc('core')->checklogin('','',true);
	$sale_live=$this->plugin_setting('sale_live');
	$goods=$sale_live;
	$roomid=pdo_getcolumn('core_account_wxapp_live_rooms',array('live_id'=>$goods['live_id'],'live_status'=>101,'uniacid'=>$_SESSION['uniacid']),'roomid');
	$rooms=pdo_get('core_account_wxapp_live_rooms',array('roomid'=>$roomid,'uniacid'=>$_SESSION['uniacid']));
	if(!$rooms && $roomid){
		$rooms=$this->model('live')->addrooms($roomid,$goods['live_id']);
	}
	if(!$rooms && $_SESSION['room_id']){
		$rooms=pdo_get('core_account_wxapp_live_rooms',array('roomid'=>$_SESSION['room_id'],'uniacid'=>$_SESSION['uniacid']));
	}
	$roomid=$rooms['roomid'];
	if($roomid){
		$looklog=pdo_get('core_account_wxapp_live_looklog',array('live_id'=>$goods['live_id'],'roomid'=>$roomid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		$looklog=pdo_get('core_account_wxapp_live_looklog',array('live_id'=>$goods['live_id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'','id desc');
	}
	$roomid=$roomid?:$looklog['roomid'];
	if(!$looklog['id'] && $rooms){
		if($_RQ['share_openid']){
			$return['share_openid']=$_RQ['share_openid'];
			$fromuser=pdo_getcolumn('core_fans',array('type'=>1,'openid'=>$_RQ['share_openid'],'wxgid'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),'mid');
		}
		$looklog=$this->model('live')->uplooklog($rooms,$fromuser,'',true);
	}
	if($roomid){
		$looklog['uptime']=$looklog['uptime']?:TIMESTAMP;
		$looklog['looktime']=TIMESTAMP-$looklog['uptime'];
		$alllooktime=$looklog['islooktime']+$looklog['looktime'];
		$looklog['looktimefen']=intval($alllooktime/60);
		$looklog['looktimemin']=$alllooktime-$looklog['looktimefen']*60;
		$this->model('live')->getjiang($looklog);
	}
	$goods['huo_set']=$goods['lookjiang_set'];
	foreach($goods['huo_set'] as &$hv){
		$getlog=pdo_get('core_account_wxapp_live_getlog',array('getfen'=>$hv['fen'],'roomid'=>$roomid,'livegoodsid'=>$livegoodsid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($looklog['looktimefen']>=$hv['fen']){
			$hv['isnow']=1;
			if(!$getlog['id']){
				$canget=1;
			}
		}
		if(!$getlog['id']){
			$lastget=1;
		}
	}
	$looklog['canget']=$canget;
	$looklog['lastget']=$lastget;
	$return['looklog']=$looklog;
	$return['live_status']=array(0=>'未知',101=>'直播中',102=>'未开始',103=>'已结束',104=>'禁播',105=>'暂停',106=>'异常',107=>'已过期');
	$return['rooms']=$rooms;
	$return['goods']=$goods;
	$return['member']=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','paycode','paycodetime','avatar','username','nickname'));
	$return['roomid']=$roomid;
	$return['qrcode']=tomedia($sale_live['ewm']);
	$return['sharedata']=array(
		'title'=>$goods['title']?:'直播活动',
		'desc'=>'看直播，领奖励！'.$goods['title'],
		'link'=>custom_url('jy_h5live/live_huodong//id='.$livegoodsid,true),
		'imgUrl'=>tomedia($goods['image']),
	);
	set_html('title',$goods['title']?:'直播活动');
	exi($return);
}
if($_RQ['op']=='getjiang'){
	$livegoodsid=$_RQ['id'];
	$roomid=intval($_RQ['roomid']);
	if(!$livegoodsid){
		exi('未知的活动ID！','error');
	}
	if(!$roomid){
		exi('未知的直播间ID！','error');
	}
	$rooms=pdo_get('core_account_wxapp_live_rooms',array('roomid'=>$roomid,'uniacid'=>$_SESSION['uniacid']));
	if($rooms['live_status']!=101){
		exi('只有【直播中】的房间，才能领奖！','error');
	}
	$goods=$this->plugin_setting('sale_live');
	$looklog=pdo_get('core_account_wxapp_live_looklog',array('roomid'=>$roomid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($looklog['uptime']>time()-3*3600 && $roomid){
		$looklog['looktime']=time()-$looklog['uptime'];
		$alllooktime=$looklog['islooktime']+$looklog['looktime'];
		$looklog['looktimefen']=intval($alllooktime/60);
	}
	$goods['huo_set']=$goods['lookjiang_set'];
	foreach($goods['huo_set'] as $hv){
		$getlog=pdo_get('core_account_wxapp_live_getlog',array('getfen'=>$hv['fen'],'roomid'=>$roomid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($looklog['looktimefen']>=$hv['fen'] && !$getlog['id']){
			$isget=1;
			$data=array(
				'live_id'=>$looklog['live_id'],
				'live_roomid'=>$looklog['live_roomid'],
				'roomid'=>$roomid,
				'getfen'=>$hv['fen'],
				'fee'=>$hv['num'],
			);
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$_SESSION['uid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('core_account_wxapp_live_getlog',$data);
			cfc('core')->member_credit($_SESSION['uid'],'credit1',$hv['num'],1,'jy_weishop','看直播领奖励');
		}
	}
	if(!$isget){
		exi('领奖失败！您已领取过了或目前没有奖可领！','error');
	}
	exi('领奖成功！');
}
?>